/**
 *
 *
 * @export
 * @param {*} obj
 * @returns {string}
 * @description 获取数据类型
 */
export function getType(obj: any): string {
  const map: Record<string, string> = {
    '[object Boolean]': 'boolean',
    '[object Number]': 'number',
    '[object String]': 'string',
    '[object Function]': 'function',
    '[object Array]': 'array',
    '[object Date]': 'date',
    '[object RegExp]': 'regExp',
    '[object Undefined]': 'undefined',
    '[object Null]': 'null',
    '[object Object]': 'object',
  };
  const type: string = Object.prototype.toString.call(obj);
  return map[type];
}

// 1.判断两个对象的内容(键名键值)是否相等(浅比较)，而不是2.判断两个对象堆中的内存空间是否相等(深比较)

//2.成立的条件：obj1 === obj2; Object.is(obj1,obj2);
export function isObjectValueEqual(a: any, b: any): boolean {
  if (a === b) {
    return true;
  }

  if (
    getType(a) !== getType(b) ||
    getType(a) !== 'object' ||
    getType(b) !== 'object'
  ) {
    return false;
  }

  const aPropsNames = Object.getOwnPropertyNames(a);
  const bPropsNames = Object.getOwnPropertyNames(b);
  if (aPropsNames.length !== bPropsNames.length) return false;
  for (let i = 0; i < aPropsNames.length; i++) {
    const currPropsName = aPropsNames[i];
    const valueA = a[currPropsName];
    const valueB = b[currPropsName];
    if (!b.hasOwnProperty(currPropsName)) return false;
    if (valueA instanceof Object && valueB instanceof Object) {
      if (!isObjectValueEqual(valueA, valueB)) return false;
    } else if (valueA !== valueB) {
      return false;
    }
  }
  return true;
}
